/*
 * Copyright 2006 Robert Burrell Donkin
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */ 
package rat.license.standard;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;

import rat.license.LicenseNoteTransformer;
import rat.report.text.IPrinter;
import rat.report.text.Reportable;

public class LicenseCollationVisitor implements IStandardLicenseVisitor, Reportable {

    private final List apache;
    private final List generated;
    private final List unknown;
    private final List oasis;
    private final List w3c;
    
    public LicenseCollationVisitor() {
        apache = new ArrayList();
        generated = new ArrayList();
        unknown = new ArrayList();
        oasis = new ArrayList();
        w3c = new ArrayList();
    }
    
    public List getApache() {
        return apache;
    }

    public List getGenerated() {
        return generated;
    }

    public List getOasis() {
        return oasis;
    }

    public List getUnknown() {
        return unknown;
    }

    public List getW3c() {
        return w3c;
    }

    public void visit(ApacheSoftwareLicense20 license) {
        apache.add(license);
    }

    public void visit(JavaDocLicenseNotRequired license) {
        generated.add(license);
    }

    public void visit(OASISLicense license) {
        oasis.add(license);
    }

    public void visit(UnknownLicense license) {
        unknown.add(license);
    }

    public void visit(W3CLicense license) {
        w3c.add(license);
    }

    public void clear() {
        apache.clear();
        generated.clear();
        oasis.clear();
        unknown.clear();
        w3c.clear();
    }

    public void report(IPrinter printer) {
        print(printer, apache, "Apache Licensed", true);
        print(printer, oasis, "OASIS Licensed", true);
        print(printer, w3c, "W3C Licensed", true);
        print(printer, generated, "Generated Documents", true);
        print(printer, unknown, "Unknown Licenses", false);
    }

    private void print(IPrinter printer, Collection results, String moniker, boolean printNotes) {
        final int size = results.size();
        if (size > 0) {
            printer.printLine("  " + size + " " + moniker);
            if (printNotes) { 
                Collection notes = CollectionUtils.collect(results, LicenseNoteTransformer.getInstance());
                notes = new HashSet(notes);
                for(Iterator it=notes.iterator();it.hasNext();) {
                    Object note = it.next();
                    printer.printLine(note.toString());
                    printer.printLine("");
                }
            }
        }
    }

    public void visit(GeneratedLicenseNotRequired license) {
        generated.add(license);
    }
}
